package com.frxuexi.sign

import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener
import com.baidu.mapapi.map.MapPoi
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.frxuexi.lib_base.mvvm.BaseViewActivity
import com.frxuexi.sign.databinding.ActivityMapBinding


@Route(path = "/sign/MapActivity")
class MapActivity : BaseViewActivity<ActivityMapBinding>() {
    var map:MapView?=null
    var mbaiduMap:BaiduMap?=null
    var mLocationClient:LocationClient?=null
    override fun initData() {

    }

    override fun initView() {
        initMap()

    }

    fun initMap(){
        map=binding.map
        mbaiduMap=map?.map
        mbaiduMap?.setMyLocationEnabled(true);
        //定位初始化
        //定位初始化
        LocationClient.setAgreePrivacy(true)
        mLocationClient = LocationClient(this)

//通过LocationClientOption设置LocationClient相关参数

//通过LocationClientOption设置LocationClient相关参数
        val option = LocationClientOption()
        option.isOpenGps = true // 打开gps

        option.setCoorType("bd09ll") // 设置坐标类型

        option.setScanSpan(1000)
        option.setIsNeedLocationDescribe(true);
//设置locationClientOption

//设置locationClientOption
        mLocationClient?.setLocOption(option)

//注册LocationListener监听器

//注册LocationListener监听器
        val myLocationListener = MyLocationListener()

        mLocationClient?.registerLocationListener(myLocationListener)
//开启地图定位图层
//开启地图定位图层
        mLocationClient?.start()

        mbaiduMap?.setOnMapClickListener(object :OnMapClickListener{
            override fun onMapClick(p0: LatLng?) {
//                Log.i("===","${p0?.latitude}")
                ARouter.getInstance().build("/sign/SignActivity").withString("loc","八维学院").navigation()
                finish()
            }

            override fun onMapPoiClick(p0: MapPoi?) {

            }
        })
    }


    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || map == null) {
                return
            }
            val locData = MyLocationData.Builder()
                .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.direction).latitude(location.latitude)
                .longitude(location.longitude).build()
            val locationDescribe = location.locationDescribe //获取位置描述信息
            Log.i("===","${locationDescribe}")

            mbaiduMap?.setMyLocationData(locData)
        }
    }

    override fun onDestroy() {
        mLocationClient!!.stop()
        mbaiduMap?.setMyLocationEnabled(false)
        map?.onDestroy()
        map = null
        super.onDestroy()
    }


}